﻿using System;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Threading;
using Action = System.Action;

namespace GaleSong
{
    /// <summary>
    ///     Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public static ResourceDictionary SharedResources { get; set; }

        public MainWindow()
        {
            InitializeComponent();
            SharedResources = Resources;

            Unloaded += MainWindow_Unloaded;
        }

        void MainWindow_Unloaded(object sender, RoutedEventArgs e)
        {
            SharedResources = null;
        }
    }

    public class PageContentConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
            {
                return Binding.DoNothing;
            }
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class RibbonPartTemplateSelector : DataTemplateSelector
    {
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            var part = item as PartBase;
            if (part != null && part.Items.Any())
            {
                return (DataTemplate)MainWindow.SharedResources["RibbonPartsTemplate"];
            }
            return (DataTemplate)MainWindow.SharedResources["RibbonPartTemplate"];
        }
    }

    public class MenuPartTemplateSelector : DataTemplateSelector
    {
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            var part = item as PartBase;
            if (part != null && part.Items.Any())
            {
                return (DataTemplate)MainWindow.SharedResources["MenuPartsTemplate"];
            }
            return (DataTemplate)MainWindow.SharedResources["MenuPartTemplate"];
        }
    }

    public class ToolbarPartTemplateSelector : DataTemplateSelector
    {
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            var part = item as PartBase;
            if (part != null && part.Items.Any())
            {
                return (DataTemplate)MainWindow.SharedResources["ToolbarPartsTemplate"];
            }
            return (DataTemplate)MainWindow.SharedResources["ToolbarPartTemplate"];
        }
    }
}